package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //处理查询所有部门请求
    @RequestMapping("/list")
    @RequiredPermission(name = "部门列表",expression = "department:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";//找视图  "/WEB-INF/views/dept/list.jsp"
    }
   /*
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        List<Department> departments = departmentService.listAll();
        mv.addObject("departments",departments);
        mv.setViewName("dept/list");
        return mv;
    }*/
    //处理删除请求
    @RequestMapping("/delete")
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    public String delete(Long id){
        if(id!=null){
            departmentService.delete(id);
        }
        return "redirect:/department/list";  //重定向
    }
    //去新增或去修改页面
   /* @RequestMapping("/input")
    public String input(Long id,Model model){
        if(id!=null){
            Department department = departmentService.get(id);
            model.addAttribute("dept",department);
        }
        return "department/input";
    }*/
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门修改或新增",expression = "department:delete")
    public String saveOrUpdate(Department department){
        if(department.getId()==null){
            departmentService.save(department);
        }else{
            departmentService.update(department);
        }
        return "redirect:/department/list";
    }



}
